package com.LC;

import com.LC.Constant.RedisConstant;
import com.LC.Utils.FileUploadAndDeleteUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 自定义job，实现定时清理垃圾图片
 */
public class CleanImg {

    @Autowired
    private JedisPool jedisPool;

    public void cleanimg(){
        //根据Redis中保存的两个set集合进行差值计算，获得垃圾图片名称集合
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set != null){
            for (String imgName : set) {
                //删除七牛云服务器上的图片
                FileUploadAndDeleteUtils.deleteFileFromQiniu(imgName);
                //从Redis集合中删除图片名称(大的集合)
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
                System.out.println("自定义任务执行，清理垃圾图片" + imgName);
            }
        }

    }
}
